package com.sunlx.product.generatesql;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.sunlx.product.dictionary.AlterTableTypeEnum;
import com.sunlx.product.dictionary.DatabaseTwoEnum;
import com.sunlx.product.domain.entity.ColumnDll;
import com.sunlx.product.domain.entity.TableDll;
import com.sunlx.product.domain.vo.TableDataVo;
import com.sunlx.product.mapper.MyDb1.MyDb1Mapper;
import com.sunlx.product.mapper.MyDb2.MyDb2Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.List;

/**
 * MySQL
 * 自动装配
 * 如果数据库类型未mysql ，所有sql方法通过此类执行
 * @author Sunluxin
 * @date 2023/1/12 15:30
 */
public class MysqlSqlNormal implements SqlStrategy{

    @Resource
    private MyDb1Mapper myDb1Mapper;
    @Resource
    private MyDb2Mapper myDb2Mapper;

    @Value("${spring.datasource.MyDb1.tableSchema}")
    private String tableSchema1;

    @Value("${spring.datasource.MyDb2.tableSchema}")
    private String tableSchema2;

    /**
     * 查询数据库的表结构及字段信息
     * @param databaseTwoEnum
     * @return {@link List< TableDataVo>}
     * @author Sunluxin
     * @date 2023/1/13 15:06
     */
    @Override
    public List<TableDataVo> selectTableData(DatabaseTwoEnum databaseTwoEnum) {
        if (DatabaseTwoEnum.MAIN.equals(databaseTwoEnum)){
            List<TableDataVo> mainTableDataVos = myDb1Mapper.selectTableData_Mysql(tableSchema1);
            return mainTableDataVos;
        }else {
            List<TableDataVo> folloTableDataVos = myDb2Mapper.selectTableData_Mysql(tableSchema2);
            return folloTableDataVos;
        }
    }

    /**
     * 查找表的dll
     * @param tableName
     * @return {@link TableDll}
     * @author Sunluxin
     * @date 2023/1/13 15:06
     */
    @Override
    public TableDll selectTableDll(String tableName) {
        TableDll dll=new TableDll();
        List<TableDll> tableDlls = myDb2Mapper.selectTableDll_mysql(tableName);
        if(CollectionUtils.isNotEmpty(tableDlls)){
            dll=tableDlls.get(0);
        }
        return dll;
    }

    /**
     * 生成字段的新增、修改、删除语句
     * @param tableDataVo
     * @param alterTableTypeEnum
     * @return {@link ColumnDll}
     * @author Sunluxin
     * @date 2023/1/13 15:05
    */
    @Override
    public ColumnDll geneSql(TableDataVo tableDataVo, AlterTableTypeEnum alterTableTypeEnum,Boolean onlyComments) {
        String sql="";
        if(AlterTableTypeEnum.DELETE.equals(alterTableTypeEnum)){
            sql="alter table "+tableDataVo.getTabelName()+ " "+ alterTableTypeEnum.getType() +" "+tableDataVo.getColumnName();
        }else {
            sql="alter table "+tableDataVo.getTabelName()+ " "+ alterTableTypeEnum.getType() +" column " +tableDataVo.getColumnName()
            + " " + tableDataVo.getColumnType() + " default " +tableDataVo.getDataDefault()+" comment "+ "'"+tableDataVo.getComments()+"'"
            ;
        }
        if(tableDataVo.getNullable()){
            sql += " null ";
        }else{
            sql+=" not null ";
        }

        if(onlyComments){
             sql=" comment on column "+tableDataVo.getTabelName()+"."+tableDataVo.getColumnName()+" is '"+tableDataVo.getComments()+"' ";
        }

        return new ColumnDll(tableDataVo.getTabelName(),tableDataVo.getColumnName(),sql,alterTableTypeEnum.getLabel());
    }

    @Override
    public String getUserName(String type) {
        if("userName1".equals(type)){
            return tableSchema1;
        }else if("userName2".equals(type)){
            return tableSchema2;
        }
        return "";
    }
}
